import 'dart:math';

class StringUtil {
  static String genRandomString(
      {bool isRandomLength = true, int min = 1, int max = 6}) {
    String randomStr = "";
    int range = min;
    String seed =
        '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';

    if (isRandomLength) {
      range = Random().nextInt(max - min) + min;
    }
    for (var i = 0; i < range; i++) {
      var pos = Random().nextInt(seed.length - 1);
      randomStr += seed[pos];
    }
    return randomStr;
  }

  static String subNickname(String nickname, int length) {
    String subNickname;

    int nicknameLength = nickname.runes.length;

    if (nicknameLength > length) {
      subNickname =
          String.fromCharCodes(nickname.runes.toList().sublist(0, length));

      subNickname += '...';
    } else {
      subNickname = nickname;
    }

    return subNickname;
  }

  static String toTwoDigits(int n) {
    if (n >= 10) return '$n';
    return '0$n';
  }
}
